import QtQuick 1.1
import "../../../common/lib/kdeadmouseutils-build-desktop"

Item {
  id : mainItem
  property bool picasaMode : true
  property string modelName
  property alias model : innerModel
  signal modelReady()
  
  function loadPicasaAlbum(urlAlbum){
    innerModel.clear();
    picasaMode=true;
    modelName=urlAlbum;
    xmlModel.albumAtomUrl=urlAlbum;
    xmlModel.loadElt();
  }
  
  function loadLocalModel(dirPath){
      innerModel.clear();
      picasaMode=false;
      modelName=dirPath;
      findShell.shellAction('find',[dirPath
      ,'-name','*.jpg'
      , '-o' ,'-name','*.png' 
      , '-o' ,'-name','*.jpeg'
      //, '-printf',"'{name:'%f',path:'%p'}'"
      ]);
  }
  
  
  function indexChanged(currentIndex){
    if(!picasaMode){return;}
    
    if(innerModel.count-currentIndex < 2){
      xmlModel.avance();
      xmlModel.loadElt();
    }
  }
  
  ListModel {
        id : innerModel
  }
  ShellInvoker {
	id : findShell
	onFinished : {
	  console.log('findShell finished');
	  mainItem.modelReady();
	}
	onOutputLineChanged : {
	  console.log('===>' + outputLine);
	  innerModel.append({url:outputLine});
	}
  }
  
  XmlListModel {
	id: xmlModel
	property int startIndex : 1
	property int maxResult : 4 
	property string albumAtomUrl //: "https://picasaweb.google.com/data/feed/base/user/112103620701242175348?alt=atom&kind=photo&hl=fr&access=public"
   
	//source: albumAtomUrl +  "&start-index="+ startIndex + "&max-results=" + maxResult
	query: "/feed/entry"
	namespaceDeclarations: "declare default element namespace 'http://www.w3.org/2005/Atom';"
	XmlRole { name: "title"; query: "title/string()" }
	XmlRole { name: "pubDate"; query: "published/string()" }
	XmlRole { name: "url"; query: "content/@src/string()" ;isKey:true}
	
	onStatusChanged : {
	  //copier les elts du model
	  if (status == XmlListModel.Ready){
	      //pour une raison que j ignore, le reload ne marche pas !!!
	      console.log('append elts count==>' + count +' ,startIndex=>'+ startIndex); 
	      var wasEmpty = innerModel.count==0;
	      for(var i=0;i<count;i++){
		innerModel.append(get(i));
	      }
	      if(wasEmpty && count > 3){
		mainItem.modelReady();
	      }
	  }
	}
	
	function loadElt() {
	  source = albumAtomUrl +  "&start-index="+ startIndex + "&max-results=" + maxResult;
	}
	
	function avance() {
	  startIndex= startIndex + count;
	  console.log('avance..==>' + startIndex);
	}
    }
}

 
